<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="作物种类" prop="productCategory">
        <el-select v-model="queryParams.productCategory" placeholder="请选择作物种类" clearable>
          <el-option
            v-for="dict in sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="品种" prop="variety">
        <el-select v-model="queryParams.variety" placeholder="请选择品种" clearable>
          <el-option
            v-for="dict in sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="溯源码" prop="traceabilityCode">
        <el-input
          v-model="queryParams.traceabilityCode"
          placeholder="请输入溯源码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="质量等级" prop="qualityGrade">
        <el-select v-model="queryParams.qualityGrade" placeholder="请选择质量等级" clearable>
          <el-option
            v-for="dict in sys_job_group"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="当前状态" prop="currentStatus">
        <el-select v-model="queryParams.currentStatus" placeholder="请选择当前状态" clearable>
          <el-option
            v-for="dict in sys_notice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ncpsy:archive:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ncpsy:archive:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ncpsy:archive:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ncpsy:archive:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="archiveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="primaryId" />
      <el-table-column label="作物种类" align="center" prop="productCategory">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.productCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="品种" align="center" prop="variety">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.variety"/>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="cropName" />
      <el-table-column label="作物图片" align="center" prop="cropImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.cropImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="溯源码" align="center" prop="traceabilityCode" />
      <el-table-column label="质量等级" align="center" prop="qualityGrade">
        <template #default="scope">
          <dict-tag :options="sys_job_group" :value="scope.row.qualityGrade"/>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" align="center" prop="currentStatus">
        <template #default="scope">
          <dict-tag :options="sys_notice_type" :value="scope.row.currentStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ncpsy:archive:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ncpsy:archive:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农产品档案对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="archiveRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="作物种类" prop="productCategory">
              <el-select v-model="form.productCategory" placeholder="请选择作物种类">
                <el-option
                  v-for="dict in sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品种" prop="variety">
              <el-select v-model="form.variety" placeholder="请选择品种">
                <el-option
                  v-for="dict in sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="产品名称" prop="cropName">
              <el-input v-model="form.cropName" placeholder="请输入产品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="目前所属用户编号" prop="currentUserCode">
              <el-input v-model="form.currentUserCode" placeholder="请输入目前所属用户编号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="种植地点" prop="plantingLocation">
              <el-select v-model="form.plantingLocation" placeholder="请选择种植地点">
                <el-option
                  v-for="dict in sys_notice_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="溯源码" prop="traceabilityCode">
              <el-input v-model="form.traceabilityCode" placeholder="请输入溯源码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="作物图片" prop="cropImage">
              <image-upload v-model="form.cropImage"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否子批次" prop="isSubBatch">
              <el-radio-group v-model="form.isSubBatch">
                <el-radio
                  v-for="dict in sys_notice_type"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="作物ID" prop="cropId">
              <el-input v-model="form.cropId" placeholder="请输入作物ID" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户ID" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入用户ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="质量检测ID" prop="qualityInspectionId">
              <el-input v-model="form.qualityInspectionId" placeholder="请输入质量检测ID" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="存储条件" prop="storageConditions">
              <el-input v-model="form.storageConditions" placeholder="请输入存储条件" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="农药残留检测结果" prop="pesticideResidueResult">
              <el-input v-model="form.pesticideResidueResult" placeholder="请输入农药残留检测结果" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="质量等级" prop="qualityGrade">
              <el-select v-model="form.qualityGrade" placeholder="请选择质量等级">
                <el-option
                  v-for="dict in sys_job_group"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否归属企业" prop="isBelongToEnterprise">
              <el-select v-model="form.isBelongToEnterprise" placeholder="请选择是否归属企业">
                <el-option
                  v-for="dict in sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="当前所属企业ID" prop="currentEnterpriseId">
              <el-input v-model="form.currentEnterpriseId" placeholder="请输入当前所属企业ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前状态" prop="currentStatus">
              <el-select v-model="form.currentStatus" placeholder="请选择当前状态">
                <el-option
                  v-for="dict in sys_notice_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="保质期" prop="shelfLife">
              <el-input v-model="form.shelfLife" placeholder="请输入保质期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保质期到期期限" prop="shelfEndLife">
              <el-date-picker clearable
                v-model="form.shelfEndLife"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择保质期到期期限">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="建议零售价" prop="suggestedRetailPrice">
              <el-input v-model="form.suggestedRetailPrice" placeholder="请输入建议零售价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="种植开始日期" prop="plantingStartDate">
              <el-date-picker clearable
                v-model="form.plantingStartDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择种植开始日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="种植结束日期" prop="plantingEndDate">
              <el-date-picker clearable
                v-model="form.plantingEndDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择种植结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物流ID" prop="logisticsId">
              <el-input v-model="form.logisticsId" placeholder="请输入物流ID" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="销售方ID" prop="sellerId">
              <el-input v-model="form.sellerId" placeholder="请输入销售方ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人" prop="creator">
              <el-input v-model="form.creator" placeholder="请输入创建人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="更新人" prop="updater">
              <el-input v-model="form.updater" placeholder="请输入更新人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Archive">
import { listArchive, getArchive, delArchive, addArchive, updateArchive } from "@/api/ncpsy/management/archive"

const { proxy } = getCurrentInstance()
const { sys_yes_no, sys_notice_type, sys_job_group } = proxy.useDict('sys_yes_no', 'sys_notice_type', 'sys_job_group')

const archiveList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    productCategory: null,
    variety: null,
    traceabilityCode: null,
    qualityGrade: null,
    currentStatus: null,
  },
  rules: {
    productCategory: [
      { required: true, message: "作物种类不能为空", trigger: "change" }
    ],
    variety: [
      { required: true, message: "品种不能为空", trigger: "change" }
    ],
    currentUserCode: [
      { required: true, message: "目前所属用户编号不能为空", trigger: "blur" }
    ],
    plantingLocation: [
      { required: true, message: "种植地点不能为空", trigger: "change" }
    ],
    traceabilityCode: [
      { required: true, message: "溯源码不能为空", trigger: "blur" }
    ],
    isSubBatch: [
      { required: true, message: "是否子批次不能为空", trigger: "change" }
    ],
    cropId: [
      { required: true, message: "作物ID不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    qualityInspectionId: [
      { required: true, message: "质量检测ID不能为空", trigger: "blur" }
    ],
    suggestedRetailPrice: [
      { required: true, message: "建议零售价不能为空", trigger: "blur" }
    ],
    logisticsId: [
      { required: true, message: "物流ID不能为空", trigger: "blur" }
    ],
    sellerId: [
      { required: true, message: "销售方ID不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询农产品档案列表 */
function getList() {
  loading.value = true
  listArchive(queryParams.value).then(response => {
    archiveList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    primaryId: null,
    productCategory: null,
    variety: null,
    cropName: null,
    currentUserCode: null,
    plantingLocation: null,
    cropImage: null,
    traceabilityCode: null,
    isSubBatch: null,
    cropId: null,
    userId: null,
    qualityInspectionId: null,
    storageConditions: null,
    pesticideResidueResult: null,
    qualityGrade: null,
    packagingType: null,
    isBelongToEnterprise: null,
    currentEnterpriseId: null,
    currentStatus: null,
    shelfLife: null,
    shelfEndLife: null,
    suggestedRetailPrice: null,
    plantingStartDate: null,
    plantingEndDate: null,
    logisticsId: null,
    sellerId: null,
    creator: null,
    createTime: null,
    updater: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("archiveRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.primaryId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加农产品档案"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _primaryId = row.primaryId || ids.value
  getArchive(_primaryId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改农产品档案"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["archiveRef"].validate(valid => {
    if (valid) {
      if (form.value.primaryId != null) {
        updateArchive(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addArchive(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _primaryIds = row.primaryId || ids.value
  proxy.$modal.confirm('是否确认删除农产品档案编号为"' + _primaryIds + '"的数据项？').then(function() {
    return delArchive(_primaryIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ncpsy/archive/export', {
    ...queryParams.value
  }, `archive_${new Date().getTime()}.xlsx`)
}

getList()
</script>
